home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / gnu / a2_0b_Emacs_sr.lha / Emacs-19.25 / site-lisp / cc-compat.el next >
Lisp/Scheme  |  1994-09-10  |  5KB  |  156 lines

  1. ;;; cc-compat.el --- cc-mode compatibility with c-mode.el confusion
  2.  
  3. ;; Author: 1994 Barry A. Warsaw, Century Computing, Inc. <bwarsaw@cen.com>
  4. ;; Maintainer:    bwarsaw@cen.com
  5. ;; Created:       August 1994
  6. ;; Version:       1.2
  7. ;; Last Modified: 1994/08/10 17:54:53
  8. ;; Keywords: C++ C Objective-C cc-mode
  9.  
  10. ;; Copyright (C) 1994 Barry A. Warsaw
  11.  
  12. ;; This file is not part of GNU Emacs.
  13.  
  14. ;; This program is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2 of the License, or
  17. ;; (at your option) any later version.
  18. ;; 
  19. ;; This program is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ;; GNU General Public License for more details.
  23. ;; 
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with this program; if not, write to the Free Software
  26. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  
  28. ;;; Commentary:
  29. ;;
  30. ;; Boring old c-mode.el (BOCM) is confusion and brain melt. cc-mode.el
  31. ;; is clarity of thought and purity of chi. If you are still unwilling
  32. ;; to accept enlightenment, this might help, or it may prolong your
  33. ;; agony.
  34. ;;
  35. ;; To use, add the following to your c-mode-hook:
  36. ;;
  37. ;; (require 'cc-compat)
  38. ;; (c-set-style "BOCM")
  39.  
  40. ;; LCD Archive Entry:
  41. ;; cc-compat|Barry A. Warsaw|bwarsaw@cen.com
  42. ;; |cc-mode compatibility with c-mode.el confusion
  43. ;; |1994/08/10 17:54:53|1.2|
  44.  
  45. ;;; Code:
  46.  
  47.  
  48. ;; In case c-mode.el isn't loaded
  49. (defvar c-indent-level 2
  50.   "*Indentation of C statements with respect to containing block.")
  51. (defvar c-brace-imaginary-offset 0
  52.   "*Imagined indentation of a C open brace that actually follows a statement.")
  53. (defvar c-brace-offset 0
  54.   "*Extra indentation for braces, compared with other text in same context.")
  55. (defvar c-argdecl-indent 5
  56.   "*Indentation level of declarations of C function arguments.")
  57. (defvar c-label-offset -2
  58.   "*Offset of C label lines and case statements relative to usual indentation.")
  59. (defvar c-continued-statement-offset 2
  60.   "*Extra indent for lines not starting new statements.")
  61. (defvar c-continued-brace-offset 0
  62.   "*Extra indent for substatements that start with open-braces.
  63. This is in addition to c-continued-statement-offset.")
  64.  
  65.  
  66.  
  67. ;; these offsets are taken by brute force testing c-mode.el, since
  68. ;; there's no logic to what it does.
  69. (let* ((offsets
  70.     '((defun-block-intro     . cc-block-intro-offset)
  71.       (statement-block-intro . cc-block-intro-offset)
  72.       (defun-open            . 0)
  73.       (class-open            . 0)
  74.       (inline-open           . c-brace-offset)
  75.       (block-open            . c-brace-offset)
  76.       (block-close           . cc-block-close-offset)
  77.       (brace-list-open       . c-brace-offset)
  78.       (substatement-open     . cc-substatement-open-offset)
  79.       (substatement          . c-continued-statement-offset)
  80.       (knr-argdecl-intro     . c-argdecl-indent)
  81.       (case-label            . c-label-offset)
  82.       (access-label          . c-label-offset)
  83.       (label                 . c-label-offset)
  84.       )))
  85.   (if (assoc "BOCM" c-style-alist)
  86.       (setcdr (assoc "BOCM" c-style-alist)
  87.           (list (cons 'c-offsets-alist offsets)))
  88.     (setq c-style-alist
  89.       (cons (list "BOCM" (cons 'c-offsets-alist offsets))
  90.         c-style-alist)))
  91.   )
  92.  
  93. (defun cc-block-intro-offset (langelem)
  94.   ;; taken directly from calculate-c-indent confusion
  95.   (save-excursion
  96.     (c-backward-syntactic-ws)
  97.     (if (= (preceding-char) ?{)
  98.     (forward-char -1)
  99.       (goto-char (cdr langelem)))
  100.     (let* ((curcol (save-excursion 
  101.              (goto-char (cdr langelem))
  102.              (current-column)))
  103.       (bocm-lossage
  104.        ;; If no previous statement, indent it relative to line
  105.        ;; brace is on.  For open brace in column zero, don't let
  106.        ;; statement start there too.  If c-indent-level is zero,
  107.        ;; use c-brace-offset + c-continued-statement-offset
  108.        ;; instead.  For open-braces not the first thing in a line,
  109.        ;; add in c-brace-imaginary-offset.
  110.        (+ (if (and (bolp) (zerop c-indent-level))
  111.           (+ c-brace-offset c-continued-statement-offset)
  112.         c-indent-level)
  113.           ;; Move back over whitespace before the openbrace.  If
  114.           ;; openbrace is not first nonwhite thing on the line,
  115.           ;; add the c-brace-imaginary-offset.
  116.           (progn (skip-chars-backward " \t")
  117.              (if (bolp) 0 c-brace-imaginary-offset))
  118.           ;; If the openbrace is preceded by a parenthesized exp,
  119.           ;; move to the beginning of that; possibly a different
  120.           ;; line
  121.           (progn
  122.         (if (eq (preceding-char) ?\))
  123.             (forward-sexp -1))
  124.         ;; Get initial indentation of the line we are on.
  125.         (current-indentation)))))
  126.       (- bocm-lossage curcol))))
  127.  
  128.  
  129. (defun cc-block-close-offset (langelem)
  130.   (save-excursion
  131.     (let* ((here (point))
  132.        bracep 
  133.        (curcol (progn
  134.              (goto-char (cdr langelem))
  135.              (current-column)))
  136.        (bocm-lossage (progn
  137.                (goto-char (cdr langelem))
  138.                (if (= (following-char) ?{)
  139.                    (setq bracep t)
  140.                  (goto-char here)
  141.                  (beginning-of-line)
  142.                  (backward-up-list 1)
  143.                  (forward-char 1)
  144.                  (c-forward-syntactic-ws))
  145.                (current-column))))
  146.       (- bocm-lossage curcol
  147.      (if bracep 0 c-indent-level)))))
  148.       
  149.  
  150. (defun cc-substatement-open-offset (langelem)
  151.   (+ c-continued-statement-offset c-continued-brace-offset))
  152.  
  153.  
  154. (provide 'cc-compat)
  155. ;;; cc-compat.el ends here
  156.